package com.stupidzhe.vbreview.controller.stu;

import com.stupidzhe.vbreview.bean.ResultBean;
import com.stupidzhe.vbreview.domain.Student;
import com.stupidzhe.vbreview.domain.Teacher;
import com.stupidzhe.vbreview.service.StudentService;
import com.stupidzhe.vbreview.service.TeacherBaseService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/stu/email")
public class StudentEmailController {


    private final StudentService studentService;
    private final TeacherBaseService teacherBaseService;

    public StudentEmailController(StudentService studentService, TeacherBaseService teacherBaseService) {
        this.studentService = studentService;
        this.teacherBaseService = teacherBaseService;
    }

    @RequestMapping(value = "/")
    public String mainView(HttpServletRequest request, Model model) {
        Student student = (Student) request.getAttribute("student");
        model.addAttribute("student", student);
        Teacher teacher = teacherBaseService.getTeacherByClassId(student.getClassId());
        model.addAttribute("teacher", teacher);
        return "stu/stu.email";
    }
    @ResponseBody
    @RequestMapping(value = "/", method = RequestMethod.POST)
    public ResultBean edit(@RequestParam("email")String email, HttpServletRequest request) {
        Student student = (Student) request.getAttribute("student");
        return studentService.edit(student, email);
    }
}
